import types from './actionTypes'
import axios from 'axios'
import { fromJS } from "immutable";

const changeHomeData = result => ({
  type: types.CHANGE_DATA,
  topicList: result.topicList,
  articleList: result.articleList,
  recommendList: result.recommendList
})
export const getHomeInfo = () => {
  return dispatch => {
    axios.get('/api/home.json').then(res => {
      const result = res.data.data
      const action = changeHomeData(result)
      dispatch(action)
    }).catch(e => {
      console.log(e)
    })
  }
}
const addHomeList = (list, nextPage) => ({
  type: types.ADD_HOME_LIST,
  list: fromJS(list),
  nextPage
})
export const getMoreList = (page) => {
  return (dispatch) => {
    axios.get('/api/homeList.json?page='+page).then(res => {
      const result = res.data.articleList
      const action = addHomeList(result, page+1)
      dispatch(action)
    }).catch(e => {
      console.log(e)
    })
  }
}
export const toggleTopShow = show => ({
  type: types.TOGGLE_TOP_SHOW,
  showScroll: show
})
